package com.jrx.anytxn.reporting.job.lsFile.config;

import com.jrx.anytxn.reporting.config.LSFileConfig;
import com.jrx.anytxn.reporting.constant.ReportingConstant;
import com.jrx.anytxn.reporting.job.reportingFile.config.MergeReportingFileConfiguration;
import com.jrx.anytxn.reporting.service.ILSFileService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 临商文件合并上传
 *
 * @author liuyang
 * @date 2021-03-19
 */
@Configuration
public class MergeLSFileConfiguration {

    private static final Logger logger = LoggerFactory.getLogger(MergeReportingFileConfiguration.class);

    @Autowired
    public JobBuilderFactory jobBuilderFactory;
    @Autowired
    public StepBuilderFactory stepBuilderFactory;
    @Autowired
    private LSFileConfig fileConfig;
    @Autowired
    private ILSFileService fileService;

    @Bean
    public Job mergeLSFileJob(){
        return jobBuilderFactory.get("mergeLSFileJob")
                .start(mergeLSFileStep())
                .next(transLSFileStep())
                .build();
    }

    @Bean
    public Step mergeLSFileStep() {
        return stepBuilderFactory.get("mergeLSFileStep")
                .tasklet(mergeLSFileStepTasklet())
                .build();
    }

    @Bean
    public Step transLSFileStep() {
        return stepBuilderFactory.get("transLSFileStep")
                .tasklet(transLSFileStepTasklet())
                .build();
    }

    @Bean
    @StepScope
    public Tasklet mergeLSFileStepTasklet() {
        return (stepContribution, chunkContext) -> {
            logger.info("临商文件合并开始");
            fileService.mergeLSFile(fileConfig.getLsFilePath(), fileConfig.getLsOutPath());
            logger.info("临商文件合并处理完成");
            return RepeatStatus.FINISHED;
        };
    }

    @Bean
    @StepScope
    public Tasklet transLSFileStepTasklet() {
        return (stepContribution, chunkContext) -> {
            logger.info("临商文件传输至NTFS开始");
            fileService.transLSFile(fileConfig.getLsOutPath(), fileConfig.getLsNftsPath(), ReportingConstant.NTFS_LS_ZXBS);
            logger.info("临商文件传输至NTFS完成");
            return RepeatStatus.FINISHED;
        };
    }
}
